﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Collections;

namespace System.Web.Mvc
{
    public static class TreeExtensions
    {
        /// <summary>
        /// 树形脚本引用
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static HtmlString TreeScript(this UrlHelper helper)
        {
            const string template = " <script src='{0}' type='text/javascript'></script>";
            const string templatelink = "<link  href='{0}'  type='text/css'  rel='stylesheet' />";

            string js = UrlExtensions.GetUrlString(helper, template, "zTree/js/jquery.ztree.core-3.5.min.js");
            string css = UrlExtensions.GetUrlString(helper, templatelink, "zTree/css/zTreeStyle/zTreeStyle.css");

            return new HtmlString(js + css);
        }



    }

    /// <summary>
    /// 树形
    /// </summary>
    public class zTree
    {
        string _val;

        public string val
        {
            get { return _val; }
            set { _val = value; }
        }
        int _id;

        public int id
        {
            get { return _id; }
            set { _id = value; }
        }

        int _pid;

        public int pId
        {
            get { return _pid; }
            set { _pid = value; }
        }

        string _name;

        public string name
        {
            get { return _name; }
            set { _name = value; }
        }

        bool _open = false;

        public bool open
        {
            get { return _open; }
            set { _open = value; }
        }

    }

    /// <summary>
    /// 树形
    /// </summary>
    public class zTreeExt
    {
        int i = 0;
        Hashtable hash = new Hashtable();


        private IList<zTree> zlist = new List<zTree>();

        public IList<zTree> List
        {
            get { return zlist; }
            set { zlist = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="pid">上级</param>
        /// <param name="name">名称</param>
        /// <param name="open">是否打开</param>
        public void Add(string id, string pid, string name, bool open)
        {
            zTree info = new zTree();
            if (hash.ContainsKey(id) == false)
            {
                hash[id] = (i++);
            }

            if (hash.ContainsKey(pid) == false)
            {
                hash[pid] = (i++);
            }

            info.id = Convert.ToInt32(hash[id]);
            info.pId = Convert.ToInt32(hash[pid]);
            info.name = name;
            info.open = open;
            info.val = id;

            List.Add(info);
        }

    }
}
